package server;

import java.io.*;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Igor Demidov
 * Date: 19.02.13
 * Time: 12:13
 */
public class Database {
    /*private static List<SimpleUser> data = new LinkedList<SimpleUser>();
    final static String path = "C:\\Users\\olymp\\IdeaProjects\\mail-demidov\\data.ser";
    private static FileInputStream fileIn;
    private static FileOutputStream fileOut;
    private static ObjectInputStream in;
    private static ObjectOutputStream out;

    public Database() {
        try {
            fileIn = new FileInputStream(path);
            in = new ObjectInputStream(fileIn);
            data = (List<SimpleUser>) in.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileIn.close();
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.err.println("Database size: " + data.size());
        for (SimpleUser user : data) {
            System.err.println(user);
        }
    }

    private static void updateDatabase() {
        try {
            fileOut = new FileOutputStream(path);
            out = new ObjectOutputStream(fileOut);
            out.writeObject(data);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileOut.close();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static SimpleUser findUser(String login) {
        for (SimpleUser user : data) {
            if (user.login.equals(login)) {
                return user;
            }
        }
        return null;
    }

    public static boolean addUser(String login, String password) {
        if (findUser(login) != null) {
            return false;
        } else {
            data.add(new SimpleUser(login, password));
            updateDatabase();
            return true;
        }
    }

    public static SimpleUser tryLogin(String login, String password) {
        SimpleUser user = findUser(login);
        if (user != null && user.password.equals(password)) {
            return user;
        } else {
            return null;
        }
    }

    public static boolean sendEmail(String from, String to, String topic, String body) {
        Email email = new Email(from, to, new Date(), topic, body);
        SimpleUser userFrom = findUser(from);
        SimpleUser userTo = findUser(to);
        if (userFrom != null && userTo != null) {
            userFrom.outbox.add(email);
            userTo.inbox.add(email);
            updateDatabase();
            return true;
        } else {
            return false;
        }
    }

    public static List<Email> receiveEmails(SimpleUser user) {
        return user.inbox;
    }        */
}
